{% extends "../page.html" %}

{% block content %}
<form type="yunj" id="{{ data.builderId }}"></form>
<script type="text/javascript">
    layui.use(["jquery", 'yunj'], function () {
        let win = window;
        let doc = document;
        let $ = layui.jquery;

        class FormHandler{
            constructor(args) {
                this.args = args;
                this.formBuilderId = args.formBuilderId;
                this.isEdit = args.isEdit;
                this.departmentPostionRoleIdsApi = args.departmentPostionRoleIdsApi;
                this.isRenderDone = false;
                this.init();
            }

            init() {
                let that = this;
                that.setEventBind();
            }

            // 处理部门岗位相关字段的显示隐藏
            handleDepartmentPostionIdsFieldShowHide(form) {
                let that = this;
                let departmentPostionIdsObj = form.getFieldObj('department_postion_ids');
                if (!departmentPostionIdsObj) return;
                let departmentIdsObj = form.getFieldObj('department_ids');
                // 设置部门岗位请求api
                that.setDepartmentPostionIdsOptionsApi(form,departmentPostionIdsObj);
                // 重新加载岗位值
                let oldDepartmentPostionIds = departmentPostionIdsObj.getValue();
                if (oldDepartmentPostionIds === "") {
                    oldDepartmentPostionIds = [];
                }
                departmentPostionIdsObj.setValue(oldDepartmentPostionIds);
                let newDepartmentPostionIds = departmentPostionIdsObj.getValue();
                if (newDepartmentPostionIds === "") {
                    newDepartmentPostionIds = [];
                }
                // 处理岗位角色追加/删除
                let appendDepartmentPostionIds = yunj.arrayDiff(newDepartmentPostionIds,oldDepartmentPostionIds);
                if (appendDepartmentPostionIds.length > 0) {
                    // 追加
                    that.handleDepartmentPostionRole(form,appendDepartmentPostionIds,"append");
                } else {
                    let removeDepartmentPostionIds = yunj.arrayDiff(oldDepartmentPostionIds, newDepartmentPostionIds);
                    if (removeDepartmentPostionIds.length > 0) {
                        // 删除
                        that.handleDepartmentPostionRole(form,appendDepartmentPostionIds,"remove");
                    }
                }
                // 设置岗位字段是否显示
                that.setFieldShowHide(departmentPostionIdsObj,departmentIdsObj.getValue().length > 0);
            }

            // 处理部门岗位对应的角色追加/删除
            handleDepartmentPostionRole(form,departmentPostionIds,mark){
                let that = this;
                let roleIdsObj = form.getFieldObj('role_ids');
                if (departmentPostionIds.length==0 || !roleIdsObj) {
                    return
                }
                // 获取岗位对应角色ids
                yunj.request({
                    url:that.departmentPostionRoleIdsApi,
                    type:"post",
                    data:{department_postion_ids:departmentPostionIds},
                }).then(res=>{
                    let postionRoleIds = res.data.role_ids;
                    let roleIds = roleIdsObj.getValue();
                    if (roleIds==="") {
                        roleIds = [];
                    }
                    switch(mark) {
                        case 'append':
                            roleIds = roleIds.concat(postionRoleIds);
                            roleIds = [...new Set(roleIds)];  // 去重
                            break;
                        case 'remove':
                            roleIds = yunj.arrayDiff(roleIds,postionRoleIds);
                            break;
                    }
                    roleIdsObj.setValue(roleIds);
                }).catch(err=>{
                    console.log(err);
                    yunj.error(err);
                });
            }

            // 设置部门岗位请求api
            setDepartmentPostionIdsOptionsApi(form,departmentPostionIdsObj){
                let departmentIdsObj = form.getFieldObj('department_ids');
                let departmentIds = departmentIdsObj.getValue();
                let departmentPostionApi = departmentPostionIdsObj.getOptionsApi();
                departmentPostionApi = yunj.urlPushParam(departmentPostionApi,"department_ids",departmentIds.length>0?departmentIds.join(","):"");
                departmentPostionIdsObj.setOptionsApi(departmentPostionApi);
            }

                // 设置部门岗位请求api

            // 设置字段的显示/隐藏
            setFieldShowHide(fieldObj,isShow){
                if (!fieldObj) {
                    return;
                }
                if(isShow){
                    fieldObj.boxEl.parent("div").show();
                }else{
                    fieldObj.boxEl.parent("div").hide();
                }
            }

            setEventBind(){
                let that = this

                // 绑定渲染完成后的事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_render_done`, function (e, form) {
                    that.isRenderDone = true;
                    that.handleDepartmentPostionIdsFieldShowHide(form);
                });
                // 监听部门值变更事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_department_ids_change`, function (e, field) {
                    let form = yunj.formObj(field.formId);
                    that.handleDepartmentPostionIdsFieldShowHide(form);
                });
                // 监听部门岗位渲染前执行
                $(doc).bind(`yunj_form_${that.formBuilderId}_department_postion_ids_render_before`, function (e, field) {
                    // 获取部门ids，设置请求url
                    let form = yunj.formObj(field.formId);
                    that.setDepartmentPostionIdsOptionsApi(form,field);
                });
                // 监听部门岗位值变更事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_department_postion_ids_change`, function (e, field,args) {
                    let form = yunj.formObj(field.formId);
                    if (args.hasOwnProperty("appendValues") && args.appendValues.length > 0) {
                        that.handleDepartmentPostionRole(form,args.appendValues,"append");
                        return;
                    }
                    if (args.hasOwnProperty("removeValues") && args.removeValues.length > 0) {
                        that.handleDepartmentPostionRole(form,args.removeValues,"remove");
                        return;
                    }
                });
            }

        }

        $(doc).ready(function () {
            new FormHandler({
                formBuilderId: '{{ data.builderId }}',
                isEdit: {% if data.isEdit %}true{% else %}false{% endif %},
                departmentPostionRoleIdsApi: '{{ data.departmentPostionRoleIdsApi }}',
            });
        });
    });
</script>
{% endblock %}